package simulation;

import streaming.Host;
import streaming.StreamingConfig;

/**
 * This class executes the periodic send event, i.e., the event
 * where the node send data to attend its neighbors request
 * 
 * @author emanuel
 */

public class PeriodicSend implements EventHandler {

	Host host;
	Simulator sim;
	double interval;
	
	public PeriodicSend(Host newHost) {
		this.setHost(newHost);
		this.setSim(Simulator.getInstance());
		this.setInterval(1 / (StreamingConfig.MCASTRATE * StreamingConfig.NONSENDERATTENDRATIO));
	}
	
	public void addEvent() {
		double time = this.getSim().getTime() + this.getInterval();
		if (Math.abs(time - Math.round(time)) < 0.00001)
			time = Math.round(time);
		this.getSim().addEvent(time, this);
	}
	
	public void run(double time) {
		if (this.getHost().getId() == -1)
			return;
		this.getHost().sendPacket();
	}

	/* Getters and Setters */
	
	public Host getHost() {
		return host;
	}

	public void setHost(Host host) {
		this.host = host;
	}

	public Simulator getSim() {
		return sim;
	}

	public void setSim(Simulator sim) {
		this.sim = sim;
	}

	public double getInterval() {
		return interval;
	}
	
	/* This method must exist to simulate the attackers */
	public void setInterval(double newInterval) {
		this.interval = newInterval;
	}
}
